package org.microservice.system.controller;

import lombok.extern.slf4j.Slf4j;
import org.microservice.common.base.BaseControllerImpl;
import org.microservice.common.entity.SysZuulRoute;
import org.microservice.common.util.PageUtils;
import org.microservice.common.util.Query;
import org.microservice.common.util.R;
import org.microservice.common.util.constant.MqQueueConstant;
import org.microservice.system.dao.SysZuulRouteDao;
import org.microservice.system.service.SysZuulRouteService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: 齐丹枫
 * Date: 2018/6/25
 * Time: 9:04
 * To change this template use File | Settings | File Templates.
 * Description:
 */
@RestController
@RequestMapping("/zuul")
@Slf4j
public class SysZuulRouteController extends BaseControllerImpl<SysZuulRouteDao,SysZuulRoute> {

    @Autowired
    private SysZuulRouteService sysZuulRouteService;

    @Autowired
    private RabbitTemplate rabbitTemplate;


    @GetMapping("/getBydelFlag")
    public R getBydelFlag(boolean delFlag){
        List<SysZuulRoute> routes = sysZuulRouteService.getBydelFlag(delFlag);
        return R.ok().put("data",routes);
    }



    public R getListPage(@RequestBody Map<String,Object> params){
        Query query = new Query<>(params);
        List<SysZuulRoute> routes = sysZuulRouteService.getZuulRouteListPage(query);

        int total = sysZuulRouteService.getZuulRouteListPageTotal(query);
        PageUtils pageUtils = new PageUtils(routes, total, query.getLimit(), query.getCurrPage());
        return R.ok().put("data",pageUtils);
    }



    public R save(@RequestBody SysZuulRoute sysZuulRoute){
        sysZuulRouteService.insert(sysZuulRoute);
        rabbitTemplate.convertAndSend(MqQueueConstant.ROUTE_CONFIG_CHANGE,"0");
        return R.ok();
    }


    public R update(@RequestBody SysZuulRoute sysZuulRoute){
        sysZuulRouteService.updateById(sysZuulRoute);
        rabbitTemplate.convertAndSend(MqQueueConstant.ROUTE_CONFIG_CHANGE,"0");
        return R.ok();
    }

}
